import time

from selenium.webdriver.common.by import By
from common.common import driver

#登录测试
class TestSignin:
    url=""
    driver=""

    def __init__(self):
        self.driver=driver.driver
        self.url="http://127.0.0.1:58080/sign-in.html"
        self.driver.get(self.url)

    #正确登录用例
    def Sucsignin(self):
        # 添加屏幕截图
        driver.getScreeShot()
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()

        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("wyr")
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        # 点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 跳转至主页，找到主页标题即登录成功
        time.sleep(0.5)
        text=self.driver.find_element(By.CSS_SELECTOR,"#article_list_board_title").text
        assert text=="首页"
        # 添加屏幕截图
        driver.getScreeShot()

    def Failsignin(self):
        # 填写错误的账号密码，点击登录
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("666")
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("666")
        # 点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 若显示用户名或密码错误，则为登录失败
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right")
        # 添加屏幕截图
        driver.getScreeShot()

        # 只填写账号，点击登录
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wyr")
        # 点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 若显示密码为空，则登录失败
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-2 > div > div")
        # 添加屏幕截图
        driver.getScreeShot()

        # 只填写密码，点击登录
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # 点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 若显示用户名不能为空，则登录失败
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-3 > div")
        # 添加屏幕截图
        driver.getScreeShot()

        # 填写正确的账号，错误的密码，点击登录
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wyr")
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("666")
        # 点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 若显示用户名或密码错误，则为登录失败
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right")
        # 添加屏幕截图
        driver.getScreeShot()

        # 填写错误的账号，正确的密码，点击登录
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("666")
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # 点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 若显示用户名或密码错误，则为登录失败
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right")
        # 添加屏幕截图
        driver.getScreeShot()

        # 填写正确的账号密码，不点击登录
        # 清空所有输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入用户名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("wyr")
        # 输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # 若仍为登录界面，找到用户登录即为登录失败
        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.page.page-center > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")
        # 添加屏幕截图
        driver.getScreeShot()

